<template>
  <div class="mcontainer">
    <div class="serach-option">
      <el-form :model="queryParams" ref="queryRef" :inline="true" class="search-form" @submit.prevent>
        <el-form-item label="需求id" prop="requirementItemId">
          <el-input v-model="queryParams.requirementItemId" placeholder="请输入需求id" clearable />
        </el-form-item>
        <div v-show="false">
          <el-form-item label="需求id" prop="pageNum"></el-form-item>
          <el-form-item label="需求id" prop="pageSize"></el-form-item>
        </div>
        
        <!-- <el-form-item label="需求名称">
          <el-input v-model="queryParams.reqName" placeholder="请输入需求名称" clearable />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="getList">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- <div class="createValue">
      <el-button type="primary" icon="Download" @click="handleButtonClick('download')">下载</el-button>
    </div> -->

    <div class="table-container">
      <el-table :data="tableData" @selection-change="handleSelectionChange" border>
        <!-- <el-table-column type="selection" width="55" fixed></el-table-column> -->
        <el-table-column prop="requirementItemId" label="需求id" min-width="200" fixed></el-table-column>
        <el-table-column prop="reqCodeId" label="需求代码id" width="200"></el-table-column>
        <el-table-column prop="modelId" label="模型" min-width="250">
          <template #default="scope">
            <div v-for="(item, ind) in handleModel(scope.row)" :key="ind">
              {{ item.type }}<span>__</span>{{ item.code }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="testCaseId" label="测试用例id" width="200">
          <template #default="scope">
            <div v-for="(item, ind) in (scope.row.testCase || [])" :key="ind">
              {{ item.code }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="testProcedureId" label="测试规程id" width="200"></el-table-column>
        <el-table-column prop="testResultId" label="测试结果id" width="200">
          <template #default="scope">
            <div v-for="(item, ind) in (scope.row.testReport || [])" :key="ind">
              {{ item.code }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="testCodeId" label="测试代码id" width="200">
          <template #default="scope">
            <div v-for="(item, ind) in (scope.row.instrCode || [])" :key="ind">
              {{ item.code }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="staticAnalysisReportId" label="静态分析报告id" width="200">
          <template #default="scope">
            <div v-for="(item, ind) in (scope.row.staticReport || [])" :key="ind">
              {{ item.code }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="180" />
        <el-table-column fixed="right" label="操作" width="110">
          <template #default="scope">
            <el-button link type="primary" @click="handleButtonClick('view', scope.row)">查看</el-button>
            <el-button link type="primary" @click="handleButtonClick('edit', scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    <ViewDialog v-if="viewDialogVisible" v-model="viewDialogVisible" :title="viewDialogTitle" :data="viewDialogData" :isEdit="isEdit" />
  </div>
</template>

<script setup>
import ViewDialog from './components/ViewDialog.vue';
import { ref } from 'vue';
import * as projectAPI from '@/api/project'

const queryParams = ref({
  requirementItemId: undefined,
  pageNum: 1,
  pageSize: 10
});
const loading = ref(false);
const queryRef = ref(null);
const selectedRows = ref([]);
const viewDialogVisible = ref(false);
const viewDialogTitle = ref('需求详情');
const viewDialogData = ref({});
const isEdit = ref(false);
const total = ref(0);
const tableData = ref([]);


//检索全部数据，筛选数据


const searchTableData = async () => {
  loading.value = true;
  projectAPI.getRequireList(queryParams.value).then(res => {
    let data = res.data || {};
    tableData.value = data.records || [];
    total.value = data.total || 0
  }).finally(()=>loading.false = true)
};

const getList = async () => {
  searchTableData();
};

const handleReset = () => {
  queryRef.value.resetFields();
  getList();
};

const handleButtonClick = (flag, row) => {
  switch (flag) {
    case 'add':
      break;

    case 'download':
      break;
    case 'view':
      viewDialogData.value = row;
      viewDialogVisible.value = true;
      isEdit.value = false;
      break;
    case 'edit':
      viewDialogData.value = row;
      viewDialogVisible.value = true;
      isEdit.value = true;
      break;
  }
};

const handleSelectionChange = (selection) => {
  selectedRows.value = selection;
};
const handleModel = row => {
  let Arr = [];
  Object.keys(row).map(key => {
    if(/Model/.test(key) && Array.isArray(row[key])){
      Arr.push(...row[key])
    }
  })
  
  return Arr
}
getList();
</script>

<style scoped lang="scss">
.mcontainer{
  padding: 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-width: 1200px;
}
// .serach-option {
//   padding: 20px;
// }

// .custom-add-btn {
//   margin-left: 20px;
//   margin-right: 10px;
// }
.action-buttons {
  margin-bottom: 20px;
}
.createValue {
  margin-left: 20px;
  margin-bottom: 10px;
}
</style>
